home *** CD-ROM | disk | FTP | other *** search
- Path: cs.ruu.nl!usenet
- From: piet@stego.cs.ruu.nl (Piet van Oostrum)
- Newsgroups: comp.lang.c,comp.lang.c++,gnu.gcc.help,gnu.g++.help,comp.os.msdos.djgpp
- Subject: Re: float != float and floats as return types
- Date: 30 Jan 1996 10:35:12 +0100
- Organization: Universiteit Utrecht, Dept. of Computer Science
- Sender: piet@stego.cs.ruu.nl
- Message-ID: <wz3f8yt48f.fsf@stego.cs.ruu.nl>
- References: <4ej9lb$mpc@fu-berlin.de>
- NNTP-Posting-Host: stego.cs.ruu.nl
- In-reply-to: axl@zedat.fu-berlin.de's message of Mon, 29 Jan 1996 20:12:38 GMT
- X-Newsreader: Gnus v5.0.8
-
- >>>>> axl@zedat.fu-berlin.de (Axel Thimm) (AT) writes:
-
- AT> Hello,
- AT> I am getting confused, about how C/C++ manage float binary operations,
- AT> in particular multiplication. The next C++ example gives me surprising
- AT> results:
- AT> *** cut here: begin file t_prec.cc
- AT> #include <iostream.h>
- AT> #include <iomanip.h>
- AT> #include <math.h>
- AT> float quad( float );
- AT> int main() {
- AT> for( int i=0; i<10; ++i ) {
- AT> float a, b, c;
- AT> a = i/13.123123;
- AT> b = a*a;
- AT> c = quad(a);
- AT> cout << (b - c) << '\t';
- AT> cout << (b - a*a) << '\t';
- AT> cout << (c - quad(a)) << '\n';
- AT> }
- AT> return 0;
- AT> }
- AT> float quad( float x ) { return x*x; }
- AT> *** cut here: end file t_prec.cc
- AT> (Sorry for not using C-syntax. You can substitute the "cout <<" part
- AT> with "printf" calls, I realized it to late to try it out, and
- AT> "translating" it on the fly might get typos in)
- AT> I compile with the MSDOS port of gcc as follows: gcc t_prec.cc -lgpp
- AT> So here is my output:
- AT> *** cut here: begin output
- AT> 0 0 0
- AT> 0 3.21547e-11 3.21547e-11
- AT> 0 1.28619e-10 1.28619e-10
- AT> 0 -1.25443e-09 -1.25443e-09
- AT> 0 5.14475e-10 5.14475e-10
- AT> 0 7.14357e-10 7.14357e-10
- AT> 0 -5.01772e-09 -5.01772e-09
- AT> 0 -1.46904e-08 -1.46904e-08
- AT> 0 2.0579e-09 2.0579e-09
- AT> 0 -2.02694e-09 -2.02694e-09
- AT> *** cut here: end output
- AT> At first I thought all numbers should be zeros. The middle column might
- AT> not be zero, if the compiler calculates in higher precision than what
- AT> the variables are, so an intermediate storage (b) might loose some
- AT> digits, but I cannot understand what happens with c! Both times a
- AT> function is called, that _is_not_ inlined, so in both cases the same
- AT> loss of digits should be observed.
-
- Gcc sometimes does inline function calls even if you don't say so.
- Especially with simple functions. Maybe -O0 or -fno-default-inline
- might help.
- --
- Piet van Oostrum <piet@cs.ruu.nl>
- http://www.cs.ruu.nl/~piet
-